package managedbean.SaleQuotation;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import entity.LineItem;
import entity.Product;
import entity.SaleLead;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.PhaseEvent;
import session.stateless.DiscountBeanLocal;
import session.stateless.SaleInquiryBeanLocal;
import session.stateless.SaleLeadBeanLocal;
import session.stateless.SaleQuotationBeanLocal;

@ManagedBean(name="saleQuotationManagerBean")
@ViewScoped
public class AddSaleQuotationManagerBean implements Serializable{
    public AddSaleQuotationManagerBean() {}
    
    @EJB
    private SaleQuotationBeanLocal saleQuotationBeanLocal;
    @EJB
    private SaleInquiryBeanLocal saleInquiryBeanLocal;
    @EJB
    private SaleLeadBeanLocal saleLeadBeanLocal;
    @EJB
    private DiscountBeanLocal discountBeanLocal;
    
    private Integer leadTime;
    private String registrationNumber;
    private Long salequiryId;
    private List<Integer> quantity;
    private List<LineItem> lineItems;
    private List<Double> prices;
    private String statusMessage;
    
    public void initView(PhaseEvent event){
        try{
            if(lineItems == null){
                lineItems = new ArrayList<LineItem>();

                for(Product product:saleQuotationBeanLocal.getProduct()){
                    LineItem lineItem = new LineItem();
                    lineItem.setProduct(product);
                    lineItems.add(lineItem);
                }
            }
        }
        catch(Exception ex){
            ex.printStackTrace();
        }
    }
    
        public void initView2(PhaseEvent event){
        try
        {
            if(registrationNumber==null){
                SaleLead saleLead = (SaleLead)FacesContext.getCurrentInstance().getExternalContext().getFlash().get("selectedSaleLead");
                registrationNumber = saleLead.getRegistrationNumber();
            }
            
            if(lineItems == null)
            {
                List<Product> products = saleInquiryBeanLocal.getProduct();
                lineItems = new ArrayList<LineItem>();

                for(Product product:products)
                {
                    LineItem lineItem = new LineItem();
                    lineItem.setProduct(product);
                    lineItems.add(lineItem);
                }
            }
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
    }
    
    public List<Integer> getQuantity() {
        return quantity;
    }
    public void setQuantity(List<Integer> quantity) {
        this.quantity = quantity;
    }
    
    public List<Double> getPrices(){
        return prices;
    }
    public void setPrice(List<Double> prices){
        //send to discountmodule to get final price;
        this.prices = prices;
    }

    public String getRegistrationNumber() {
        return registrationNumber;
    }

    public void setRegistrationNumber(String registrationNumber) {
        this.registrationNumber = registrationNumber;
    }

    public Long getSalequiryId() {
        return salequiryId;
    }

    public void setSalequiryId(Long salequiryId) {
        this.salequiryId = salequiryId;
    }

    public String getStatusMessage() {
        return statusMessage;
    }

    public void setStatusMessage(String statusMessage) {
        this.statusMessage = statusMessage;
    }
    
    public List<LineItem> getLineItems() {
        return lineItems;
    }
    public void setLineItems(List<LineItem> lineItems) {
        this.lineItems = lineItems;
    }
    
    public List<Product> getProduct(){
        return saleInquiryBeanLocal.getProduct();
    }
    
    public void setLeadTime(Integer leadTime){
        //Call the method from MRP to get the leadTime according to the lineItems;
        this.leadTime = leadTime;
    }
    public Integer getLeadTime(){
        return leadTime;
    }
    
    public void saveSaleQuotation(ActionEvent event) {
        try {
            if (saleLeadBeanLocal.find(registrationNumber) == null) {
                statusMessage = "Sale Lead does not exist";
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,
                        "Add New Sale Quotation Result: Unsuccessful" + statusMessage + "", ""));
            }
            else
            {
                List<LineItem> lineItemList = new ArrayList<LineItem>();
                LineItem temp = new LineItem();
                for (int i = 0; i < lineItems.size(); i++) {
                    if (lineItems.get(i).getLineItemQuantity() != null) {
                        temp = saleQuotationBeanLocal.createLineItem_Discount(lineItems.get(i).getProduct(), lineItems.get(i).getLineItemQuantity());
                        System.out.println(""+temp.getFinalPrice().toString());
                        lineItemList.add(temp);
                    }
                }
                Long saleQuotationId = saleQuotationBeanLocal.createNewSaleQuotation(registrationNumber, lineItemList, new Integer(0));
                statusMessage = "New Sale Quotation Saved Successfully";
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,
                        "Add New Sale Quotation Result:" + statusMessage + "(New Sale Quotation ID is:" + saleQuotationId + "", ""));
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

}
